<template>
	<view class="logincomponents">
		<view class='Popup' v-if="hidden">
			<view style="height:80rpx;width:80rpx;right: 0;position: absolute;" @tap="close">
				<image style="height:80rpx;width:80rpx;" :src="photo_cdn1+'zpupload/static/login/close.png'" mode="aspectFit"></image>
			</view>
			<view class='title'>欢迎使用 炎黄儿女</view>
			<image class="backgroundimage" :src="photo_cdn1+'zpupload/static/login/loginbgs1.png'"></image>
			<view class='tip'>
				<view class="title" style="font-size: 40rpx;padding:4rpx;">请授权账号等信息 </view>
				<view style="text-align:center;color: #757575;font-size: 28rpx;letter-spacing: 4rpx;margin: 20rpx 0;">
					以便为您提供更好的服务体验</view>
			</view>
			<view class='bottom flex ' style="padding:0 30rpx;">
				<button @tap="close"
					style="border-radius: 80rpx 0 0 80rpx;background: #DBDBDB;text-align: center;font-size: 32rpx;margin: 0;border: 0;box-sizing: border-box;"
					class='item grant' type="primary">
					暂不登录
				</button>
				<!-- open-type='getUserInfo' -->
				
				<!-- #ifdef MP-ALIPAY  -->
				<!-- <button style="border-radius: 0 80rpx 80rpx 0;text-align: center;font-size: 32rpx;margin: 0;"
					class='item grant' type="primary" @tap="setUserInfotwo">
					确认授权
				</button> -->
				
				<!-- <button style="border-radius: 0 80rpx 80rpx 0;text-align: center;font-size: 32rpx;margin: 0;"
					class='item grant' type="primary" @tap="tologin">
					前往登录
				</button> -->
				<!-- #endif -->
				
				
				<!-- #ifdef  H5 || APP-PLUS || MP-WEIXIN-->
				<!-- <button style="border-radius: 0 80rpx 80rpx 0;text-align: center;font-size: 32rpx;margin: 0;"
					class='item grant' type="primary" @tap="setUserInfotwo">
					确认授权
				</button> -->
				
				<button style="border-radius: 0 80rpx 80rpx 0;text-align: center;font-size: 32rpx;margin: 0;"
					class='item grant' type="primary" @tap="tologin">
					前往登录
				</button>
				<!-- #endif -->
				
				
				<!-- #ifdef MP-ALIPAY -->

				<button
				 style="border-radius: 0 80rpx 80rpx 0;text-align: center;font-size: 32rpx;margin: 0;box-sizing: border-box;"
				 class='item grant'
				
				  @tap="onGetAuthorize"  >确认授权</button>
				
				<!--  v-if="canIUseAuthButton" @getAuthorize="onGetAuthorize" open-type="getAuthorize" scope='userInfo'  onError="onAuthError"-->
				<!-- @tap="onGetAuthorize" -->
				
				<!-- #endif -->
				
				
			</view>
		</view>
		<view class='mask' v-if="hidden" @tap="close"></view>
	</view>
</template>

<script>
	
	let login =  new Promise((ret,rej)=>{
		ret();
	});
	import {wxLogin,alilogin} from "@/utils/api/api.js";
	import config from "@/utils/config.js";
	let {staticurl,photo_cdn1} = config.baseUrl;
	export default {
		props: {
			hidden: { //弹窗是否显示 true显示  false不显示
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				photo_cdn1,
				canIUseGetUserProfile: false,
				loginstatus: false,//防止重复点击
				// #ifdef MP-ALIPAY
				canIUseAuthButton: my.canIUse('button.open-type.getAuthorize')
				// #endif
			}
		},
		methods: {
			tologin(){
				uni.reLaunch({
					url: '/pagesim/center/login'
				})
			},
			//关闭弹出
			close() {
				this.$emit("close",false);
			},
			onAuthError(err){
				console.info("err?",err);
			},
			onGetAuthorize(){
				console.info("---------------666---------------------------------");
				//支付宝小程序登录
				// #ifdef MP-ALIPAY
				
				my.getAuthCode({
				  scopes: 'auth_user',
				  success: (auth) => {
				     console.log('⽀付宝授权凭证111',auth)
					 alilogin({
						 auth_code:auth.authCode,
					 }).then((res)=>{
						 if(res.code==1){
							wx.setStorageSync('userInfo', res.data.userInfo);
							wx.setStorageSync('token', res.data.userInfo.token);
							// wx.setStorageSync('session_key', res.data.userInfo.session_key);
							this.$emit("loginsucc",{});
							this.close();
						 }else{
							 this.$api.msg(res.msg);
						 }
					 })
				  },
				});
				
				// #endif
			},
			//新版的授权形式
			setUserInfotwo(e) {
				console.info("---------------1---------------------------------");
				if (this.loginstatus) {
					return false;
				}
				this.loginstatus = true;
				
				setTimeout(() => {
					this.loginstatus= false;
				}, 5000);
				
				
				// #ifdef MP-WEIXIN
				//微信小程序登录
				try {
					wx.showLoading({
						title: '请稍后...'
					});
					wx.getUserProfile({ //获取用户信息
						lang: 'zh_CN',
						desc: '用于完善会员资料',
						success: (res) => {
							var userInfo = res.userInfo;
							// console.info("userInfo",userInfo);
							wx.login({
								success: (e) => {
									let obj = {
										code: e.code,
										encryptedData: res.encryptedData,
										iv: res.iv,
										// userInfo: res.userInfo, //信息
										// login_type: 'wechat', //登陆方式 （小程序）
										session_key: wx.getStorageSync('session_key') ||'' //缓存的key 第一次没有就不传 有就传
									};
									console.info(obj,"objinfo");
									// return false;
									wxLogin(obj).then(data => {
										wx.hideLoading();
										this.close();
										this.loginstatus = false;
										if (data.data == "-99999" || data.code =='-10800' || data.data == '-99999') {
											wx.showLoading({
												title: '请稍后....'
											});
										}

										if (data.code == 1) { //成功
											wx.setStorageSync('u_userInfo', data.data.userInfo);
											wx.setStorageSync('token', data.data.userInfo.token);
											wx.setStorageSync('session_key', data.data.userInfo.session_key);
											
											this.$emit("loginsucc",{});
											
											let Imuserinfo = data.data.Imuserinfo;
											
											
											// uni.setStorageSync('userinfo', Imuserinfo);
											// this.ws.init(Imuserinfo.token, Imuserinfo.auth_token)
											
											// getUserInfo({}).then(cont => {
											// 	wx.setStorageSync('userInfo', cont.data);
											// 	wx.setStorageSync('user_id', cont.data.info.user_id);
											// 	this.triggerEvent('setUserInfo',true); //传递方法
											// }).catch(err => {})
										}else{
											uni.getStorageSync("session_key","");//失败了 直接清除之前的session_key
										}
										wx.showToast({
											title: data.msg,
											icon: 'none'
										});
									}).catch(err => {
										wx.hideLoading();
										console.info(err, "err");
										if (err.code == '-99999' || err.code == '-10800' ||
											err.data == '-99999') {
											wx.showLoading({
												title: '再次授权...'
											});
											// this.setUserInfotwo();
										} else if (err.code == '-5') {
											wx.showLoading({
												title: err.msg
											});
										}
									})
								},
								fail: (err) => {
									this.loginstatus = false;
								}
							})
						},
						fail: () => {
							this.loginstatus = false;
						}
					})

				} catch (err) {
					this.loginstatus = false;
				}
				// #endif
				
				
				//支付宝小程序登录
				// #ifdef MP-ALIPAY
				
				// #endif

			},
		}
	}
</script>

<style lang="scss">
	@import "./login.scss";
</style>
